import { getLodop } from '@/utils/LodopFuncs';
import { parseTime4 } from '@/utils';
var LODOP,
	P_ID = '',
	TaskID1,
	TaskID2,
	t,
	c = 0; //声明为全局变量
export function checkPrinter() {
	let LODOP = getLodop();
	if (!LODOP) return false;
	return true;
}
export function print(data) {
	if (Array.isArray(data)) {
		for (let i = 0; i < data.length; i++) {
			printSignal(data[i]);
		}
	} else {
		printSignal(data);
	}
}
function printSignal(data) {
	LODOP = getLodop();
	if (!LODOP) return;
	LODOP.SET_LICENSES('', 'EE0887D00FCC7D29375A695F728489A6', 'C94CEE276DB2187AE6B65D56B3FC2848', '');
	// LODOP.SET_LICENSES("","13F0BE65846276CB60111433C6280000","","");
	LODOP.PRINT_INITA('0mm', '0mm', '210mm', '148mm', '数航供应链_ERP系统_拣货单_拣货单打印');
	let { chooseNumber, commodityNum, regionName, groupName } = data;
	LODOP.SET_PRINT_PAGESIZE(2, 0, 0, 'A5');
	LODOP.SET_SHOW_MODE("LANDSCAPE_DEFROTATED",1);
	LODOP.ADD_PRINT_TABLE(112, 0, '210mm', '700px', getTableStr(data));
	LODOP.ADD_PRINT_HTM(25, '80.869%', 146, 28, `<p style="font-weight:bold;font-size:16px"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.SET_PRINT_STYLEA(0, 'Horient', 1);
	LODOP.ADD_PRINT_HTM(25, 40, 300, 34, `<p style="font-weight:bold;font-size:16px;">区域:${regionName}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(25, '45.00%', 113, 35, `<p style="font-weight:bold;font-size:16px">品规数:${commodityNum}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.SET_PRINT_STYLEA(0, 'Horient', 4);
	LODOP.ADD_PRINT_HTM(70, 42, 250, 40, `<p style="font-weight:bold;font-size:18px">单号:${chooseNumber}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(70, '45.00%', 250, 40, `<p style="font-weight:bold;font-size:16px">通道组:${groupName}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.PRINT();
	// LODOP.PRINT_DESIGN();

	// LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS", true)
	// if (LODOP.CVERSION) {
	//   LODOP.On_Return = function (TaskID, Value) {
	//     P_ID = Value;
	//     console.log("该任务走的是c-lodop方式，job代码为" + P_ID);
	//     if (P_ID != "") { c = 0; C_WaitFor(); }else { console.log("已取消打印");};
	//   };
	//   LODOP.PRINT();
	// } else {
	//   P_ID = LODOP.PRINT();
	//   console.log("该任务走的是lodop插件方式，job代码为" + P_ID);
	//   if (P_ID != "") { c = 0; WaitFor(); } else { console.log("已取消打印");};
	// };
}
// 拣货单
export function getTableStr(data) {
	let { chooseDetailsVoList } = data;
	console.log(chooseDetailsVoList);
	let style = `<style>
.print-tab1 {border-collapse: collapse;width: 700px !important;margin: 0 auto;}
.print-tab1 td,.print-tab1 th {text-align: center;border-bottom: 1px solid #000;padding: 1px;padding:10px 0 5px;}
.print-tab1 td{font-size:14px}
</style><body>`;
	let tableStr =
		"<table  class='print-tab1'><thead><tr><th>序号</th><th>货位号</th><th>商品名称</th><th>规格</th><th>单位</th><th>数量</th><th>配送规格</th><th>件数（配送规格）</th></tr></thead><tbody>" +
		chooseDetailsVoList
			.map((row, index) => {
				return `<tr>
            <td>${index + 1}</td>
            <td style="font-weight:bold">${row.positionName}</td>
            <td>${row.commodityName}</td>
            <td>${row.standard}</td>
            <td>${row.unitName}</td>
            <td style="font-weight:bold">${row.procureCount}</td>
            <td style="font-weight:bold">${row.deliveryNorms}</td>
            <td style="font-weight:bold">${row.procureNumber}</td>
          </tr>`;
			})
			.join('') +
		'</tbody></table>' +
		'</body>';
	return style + tableStr;
}

// 打印随货单
export function printFollow(data) {
	LODOP = getLodop();
	if (!LODOP) return;
	LODOP.SET_LICENSES('', 'EE0887D00FCC7D29375A695F728489A6', 'C94CEE276DB2187AE6B65D56B3FC2848', '');
	// LODOP.SET_LICENSES("","13F0BE65846276CB60111433C6280000","","");
	LODOP.PRINT_INITA('0mm', '0mm', '210mm', '148mm', '数航供应链_ERP系统_随货单打印');
	LODOP.SET_PRINT_PAGESIZE(2, 0, 0, 'A5');
	LODOP.SET_SHOW_MODE("LANDSCAPE_DEFROTATED",1);
	LODOP.ADD_PRINT_TABLE(165, 0, '210mm', 300, getFollowTableStr(data));
	LODOP.SET_PRINT_STYLEA(0, 'ALIGNMENT', 2);
	// LODOP.ADD_PRINT_HTM(25, '80.869%', 146, 28, `<p style="font-weight:bold;font-size:16px"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(25, 0, '210mm', 30, `<h1 style="text-align:center;font-family:'楷体';margin:0 auto;font-size:26px">数航供应链统一配送单</h1>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// 一维码
	LODOP.ADD_PRINT_BARCODE(10, '6%', 220, 75, '128Auto', data.followNumber);
	// 二维码
	LODOP.ADD_PRINT_BARCODE(10, '85%', 105, 105, 'QRCode', data.followNumber);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(88, 50, '100mm', 25, `<p style="font-size:16px">随货单编号：${data.followNumber}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(88, '74%', '100mm', 25, `<p style="font-size:16px">出货日期：${parseTime4(data.printTime)}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(113, 50, '183.62mm', 50, `<p style="font-size:16px;word-break:break-all;">备注：${data.remark}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(500, 45, '100mm', 25, `<p style="font-size:16px">出货人：</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(500, 245, '100mm', 25, `<p style="font-size:16px">送货人：</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(500, 445, '100mm', 25, `<p style="font-size:16px">接货人：</p>`);
	// 分页
	LODOP.ADD_PRINT_HTM(500, '170mm', '30mm', 25, `<p style="font-weight:bold;font-size:16px"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.PRINT();
	// LODOP.PRINT_DESIGN();
}

// 随货单
export function getFollowTableStr(data) {
	let { storeFollowList } = data;
	let style = `<style>
.print-tab {border-collapse: collapse;width: 700px !important;margin: 0 auto;}
.print-tab td,.print-tab th {text-align: center;border: 1px solid #000;padding: 3px;}
.print-tab td{font-size:14px}
</style><body>`;
	let tableStr =
		"<table class='print-tab'>" +
		`<thead><tr><td>出货方</td><td colspan="2" style="text-align:left !important;">${data.warehouseName}</td><td>收货方</td><td colspan="5"  style="text-align:left !important;">${data.storeName}</td></tr>` +
		"<tr><td>商品条码</td><td>商品名称</td><td style='width:45px;'>数量</td><td>规格</td><td>件数</td><td>生产日期</td></tr></thead><tbody>" +
		storeFollowList
			.map(row => {
				return `<tr>
            <td>${row.barCode}</td>
            <td>${row.commodityName}</td>
            <td>${row.procureCount}</td>
            <td>${row.deliveryNorms +'*'+ row.standard}</td>
            <td>${row.procureNumber + '件'}</td>
            <td>${parseTime4(row.manufactureTime)}</td>
          </tr>`;
			})
			.join('') +
		'</tbody></table>' +
		'</body>';
	return style + tableStr;
}

function WaitFor() {
	c = c + 1;
	console.log('正等待(JOB代码是' + P_ID + ')打印结果：' + c + '秒');
	t = setTimeout(WaitFor, 1000);
	if (LODOP.GET_VALUE('PRINT_STATUS_OK', P_ID)) {
		clearTimeout(t);
		console.log('走的lodop插件方式：PRINT_STATUS_OK判断为打印成功');
		c = 0;
		alert('打印成功！');
	}
	if (!LODOP.GET_VALUE('PRINT_STATUS_EXIST', P_ID) && c > 0) {
		clearTimeout(t);
		console.log('走的lodop插件方式：判断该该任务已经不在队列，已成功或删除任务');
		c = 0;
		alert('打印任务不在队列，成功或删除');
	} else if (c > 50) {
		clearTimeout(t);
		console.log('打印超过50秒没捕获到成功状态！');
		c = 0;
		alert('打印超过50秒没捕获到成功状态！');
	}
}

function C_WaitFor() {
	c = c + 1;
	console.log('正等待(JOB代码是' + P_ID + ')打印结果：' + c + '秒');
	t = setTimeout(C_WaitFor, 1000);
	LODOP.On_Return_Remain = true;
	LODOP.On_Return = function (TaskID, Value) {
		// console.log(Value, '@@@@@@@@@@@');
		// console.log(TaskID, 'TaskID');
		// console.log(TaskID1, 'TaskID1');
		// console.log(TaskID2, 'TaskID2');
		if (TaskID == TaskID1) {
			if (Value == 1) {
				alert(TaskID + ' ' + Value);
				clearTimeout(t);
				console.log('走的c-lodop方式：PRINT_STATUS_OK判断为打印成功');
				c = 0;
				alert('打印成功！');
			}
		} else if (TaskID == TaskID2) {
			if (Value == 0) {
				clearTimeout(t);
				console.log('走的c-lodop方式：判断该该任务已经不在队列，已成功或删除任务');
				c = 0;
				alert('打印任务不在队列，成功或删除');
			}
		}
	};
	TaskID1 = LODOP.GET_VALUE('PRINT_STATUS_OK', P_ID);
	TaskID2 = LODOP.GET_VALUE('PRINT_STATUS_EXIST', P_ID);
	if (c > 50) {
		clearTimeout(t);
		console.log('打印超过50秒没捕获到成功状态！');
		c = 0;
		alert('打印超过50秒没捕获到成功状态！');
	}
}

// 打印盘点单
export function printCheckInfo(data) {
	LODOP = getLodop();
	if (!LODOP) return;
	LODOP.SET_LICENSES('', 'EE0887D00FCC7D29375A695F728489A6', 'C94CEE276DB2187AE6B65D56B3FC2848', '');
	// LODOP.SET_LICENSES("","13F0BE65846276CB60111433C6280000","","");
	LODOP.PRINT_INITA('0mm', '0mm', '210mm', '297mm', '数航供应链_ERP系统_盘点单打印');
	LODOP.SET_PRINT_PAGESIZE(1, 0, 0, 'A4');
	LODOP.ADD_PRINT_TABLE(175, '0%', '100%', '80%', getCheckInfo(data));
	LODOP.SET_PRINT_STYLEA(0, 'ALIGNMENT', 2);
	LODOP.ADD_PRINT_HTM(25, '80.869%', 146, 28, `<p style="font-weight:bold;font-size:16px"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(20, 0, '210mm', 30, `<h1 style="text-align:center;font-family:'楷体';margin:0 auto;font-size:26px">数航供应链统一盘点单</h1>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// 一维码
	// LODOP.ADD_PRINT_BARCODE(10, "6%", 220, 75,"128Auto",data.followNumber);
	// LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
	LODOP.ADD_PRINT_HTM(70, 50, '100mm', 25, `<p style="font-size:16px">盘点人员：${data.userName}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(70, '35%', '30%', 25, `<p style="font-size:16px">盘点名称：${data.name}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(70, '65%', '100mm', 25, `<p style="font-size:16px">区域名称：${data.detailsList[0]?.region}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(100, 50, '183.09mm', 75, `<p style="font-size:16px;word-break:break-all;">备注：${data.remark}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// LODOP.ADD_PRINT_HTM(500, 45, "100mm", 25, `<p style="font-size:16px">出货人：</p>`);
	// LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
	// LODOP.ADD_PRINT_HTM(500, 245, "100mm", 25, `<p style="font-size:16px">送货人：</p>`);
	// LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
	// LODOP.ADD_PRINT_HTM(500, 445, "100mm", 25, `<p style="font-size:16px">接货人：</p>`);
	// LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
	LODOP.PRINT();
	// LODOP.PRINT_DESIGN();
}

// 盘点单
export function getCheckInfo(data) {
	let { detailsList } = data;
	// detailsList = [...detailsList, ...detailsList];
	let style = `<style>
.print-tab {border-collapse: collapse;width: 700px !important;margin: 0 auto;}
.print-tab td,.print-tab th {text-align: center;border: 1px solid #000;padding: 3px;}
.print-tab td{font-size:14px}
</style><body>`;
	let tableStr =
		"<table class='print-tab'>" +
		"<thead><tr><td>商品名称</td><td style='width:70px;'>通道名称</td><td style='width:60px;'>仓位名称</td><td>盘点数量</td><td style='width:100px;'>备注</td></tr></thead><tbody>" +
		detailsList
			.map(row => {
				return `<tr>
            <td>${row.name}</td>
            <td>${row.aisle}</td>
            <td>${row.position}</td>
            <td></td>
            <td></td>
          </tr>`;
			})
			.join('') +
		'</tbody></table>' +
		'</body>';
	return style + tableStr;
}
